
package com.hd.sup.modules.alarm.util;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hd.common.constant.BizConstant;
import com.hd.common.utils.CollectionUtils;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.Pair;
import com.hd.common.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title AlarmUtils.java
 * @Description 报警工具类
 * @Package com.hd.sup.modules.alarm.util
 * @date 2021/1/6 6:02 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
public class AlarmUtils {

    /**
     * @return com.hd.sup.core.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 根据时间类型和 传入时间字符串生成开始结束时间
     * @date 2021/2/5 9:23 上午
     * @params [date, timeFlag]
     */
    public static Pair<String, String> getStartEndDate(String dateStr, String timeFlag, boolean pre) {
        String startDateStr = "";
        String endDateStr = "";
        Pair<String, String> datePair;
        if (BizConstant.Common.DAY.key().equalsIgnoreCase(timeFlag)) {
            datePair = DateUtil.getDayScope(dateStr, pre);
            startDateStr = datePair.first;
            endDateStr = datePair.second;
        } else if (BizConstant.Common.WEEK.key().equalsIgnoreCase(timeFlag)) {
            datePair = DateUtil.getWeekScope(dateStr, pre);
            startDateStr = datePair.first;
            endDateStr = datePair.second;
        } else if (BizConstant.Common.MONTH.key().equalsIgnoreCase(timeFlag)) {
            datePair = DateUtil.getMonthScope(dateStr, pre);
            startDateStr = datePair.first;
            endDateStr = datePair.second;
        } else if (BizConstant.Common.YEAR.key().equalsIgnoreCase(timeFlag)) {
            datePair = DateUtil.getYearScope(dateStr, pre);
            startDateStr = datePair.first;
            endDateStr = datePair.second;
        }

        return Pair.makePair(startDateStr, endDateStr);
    }

    /**
     * @return boolean
     * @author chentl
     * @description 判断当前时间是否在监测时段内
     * @date 2021/1/7 10:37 上午
     * @params [startTime, endTime]
     */
    public static boolean needMonitor(Date startTime, Date endTime, Date gpsTime) {
        String now = DateUtil.formatDate(gpsTime, BizConstant.Common.DATE_FORMAT_HMS.key());
        Date nowTime = DateUtil.parse(now, BizConstant.Common.DATE_FORMAT_HMS.key());
        //不再范围内则无需 研判报警
        if (nowTime == null) {
            log.error("当前时间取值异常: gpsTime:{}", gpsTime);
            return false;
        }
        return nowTime.getTime() >= startTime.getTime() && nowTime.getTime() <= endTime.getTime();
    }


    /**
     * @return boolean
     * @author chentl
     * @description 忽略跨天数据
     * @date 2021/2/1 1:57 下午
     * @params [currentGpsTime]
     */
    public static boolean isOverDay(String currentGpsTime) {

        if (StringUtils.isBlank(currentGpsTime)) return true;

        String toDay = DateUtil.formatDate(new Date(), BizConstant.Common.DATE_FORMAT_DAY.key());
        String gpsNow = DateUtil.formatDate(DateUtil.parse(currentGpsTime, BizConstant.Common.DATE_FORMAT_SECOND.key()), BizConstant.Common.DATE_FORMAT_DAY.key());
        return !toDay.equalsIgnoreCase(gpsNow);
    }


    /**
     * @return java.util.Map < java.lang.String, java.lang.Object>
     * @author chentl
     * @description 组装趋势数据
     * @date 2021/2/18 9:45 上午
     * @params [trendData, dateScope]
     */
    public static LinkedHashMap<String, Object> groupTrendData(List<Map<String, Object>> trendData, String timeFlag, List<String> dateScope) {

        LinkedHashMap<String, Object> frameData = Maps.newLinkedHashMap();
        for (Map<String, Object> out : trendData) {
            String tt = ValueUtils.parseString(out.get("tt"));
            List<Map<String, Object>> outList = Lists.newArrayList();
            for (Map<String, Object> in : trendData) {
                String key = ValueUtils.parseString(in.get("tt"));
                if (key.equals(tt)) {
                    outList.add(in);
                }

            }
            frameData.put(tt, outList);
        }


        //无数据则补零 使用新容器顺序接收 空间换时间
        LinkedHashMap<String, Object> frameDataFilled = Maps.newLinkedHashMap();

        dateScope.forEach(scope -> {
            List<Map<String, Object>> list = (List<Map<String, Object>>) frameData.get(scope);

            if (CollectionUtils.isNotEmpty(list)) {
                Map<String, Object> zeroMap = Maps.newHashMap();
                for (BizConstant.AlarmType alarmType : BizConstant.AlarmType.values()) {
                    //超速报警跳过
                    if (alarmType.equals(BizConstant.AlarmType.OVER_SPEED)) continue;
                    AtomicBoolean contain = new AtomicBoolean(false);
                    list.forEach(one -> {
                        if (alarmType.key().equals(ValueUtils.parseString(one.get("alarm_type")))) contain.set(true);
                    });
                    if (!contain.get()) {
                        zeroMap.put("tt", scope);
                        zeroMap.put("alarm_sum", 0);
                        zeroMap.put("alarm_type", alarmType.key());
                        zeroMap.put("verify_sum", 0);
                        zeroMap.put("handle_sum", 0);
                        list.add(zeroMap);
                    }
                }
                frameDataFilled.put(scope, list);
            } else {
                List<Map<String, Object>> zeroList = Lists.newArrayList();
                for (BizConstant.AlarmType alarmType : BizConstant.AlarmType.values()) {
                    //超速报警跳过
                    if (alarmType.equals(BizConstant.AlarmType.OVER_SPEED)) continue;
                    Map<String, Object> zeroMap = Maps.newHashMap();
                    zeroMap.put("tt", scope);
                    zeroMap.put("alarm_type", alarmType.key());
                    zeroMap.put("alarm_sum", 0);
                    zeroMap.put("verify_sum", 0);
                    zeroMap.put("handle_sum", 0);
                    zeroList.add(zeroMap);
                }
                frameDataFilled.put(scope, zeroList);
            }
        });

        //以 alarm_type 为legend 重新组装数据
        LinkedHashMap<String, Object> resMap = Maps.newLinkedHashMap();
        if (CollectionUtil.isNotEmpty(frameDataFilled)) {
            for (BizConstant.AlarmType key : BizConstant.AlarmType.values()) {
                if (key.equals(BizConstant.AlarmType.OVER_SPEED)) continue;
                List<Map<String, Object>> resList = Lists.newArrayList();

                for (Map.Entry<String, Object> entry : frameDataFilled.entrySet()) {
                    List<Map<String, Object>> mapList = (List<Map<String, Object>>) entry.getValue();

                    for (Map<String, Object> one : mapList) {
                        String alarm_type = ValueUtils.parseString(one.get("alarm_type"));
                        if (alarm_type.equals(key.key())) {
                            resList.add(one);
                        }
                    }
                    resMap.put(key.key(), resList);
                }
            }
        }

        return resMap;
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 获取趋势日期幅度
     * @date 2021/2/18 10:15 上午
     * @params [timeFlag]
     */
    public static String getFrame(String timeFlag) {
        if (BizConstant.Common.DAY.key().equalsIgnoreCase(timeFlag)) return "DAY";
        if (BizConstant.Common.WEEK.key().equalsIgnoreCase(timeFlag)) return "WEEK";
        else if (BizConstant.Common.MONTH.key().equalsIgnoreCase(timeFlag)) return "MONTH";
        else if (BizConstant.Common.YEAR.key().equalsIgnoreCase(timeFlag)) return "YEAR";
        else return "";
    }

    public static void main(String[] args) {
        Date gpsTime = DateUtil.parseTime("2021-01-19 11:07:11");
        Date startTime = DateUtil.parseTime("2021-01-19 06:17:38");
        String duration = DateUtil.getDiffSeconds(gpsTime, startTime);
        String result = DateUtil.getDateDiffHmsTemplate(duration);

        System.out.println(result);
    }

}
